package com.ydd.widegt;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ydd.R;
import com.ydd.adapter.PhotoViewPagerAdapter;
import com.ydd.model.recommand.RecommandFooterValue;
import com.ydd.model.recommand.RecommandHeadValue;
import com.ydd.moudle.home.StoryDetailActivity;
import com.ydd.moudle.home.viewpagerindictor.CirclePageIndicator;
import com.ydd.util.glide.GlideManager;

import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

/**
 * Created by tao on 17-3-20.
 */

public class HomeHeaderLayout extends RelativeLayout {
    private Context mContext;
    /**
     * UI
     */
    private RelativeLayout mRootView;
    private AutoScrollViewPager mViewPager;
    private CirclePageIndicator mPagerIndictor;
    private TextView mHotView;
    private PhotoViewPagerAdapter mAdapter;
    private ImageView[] mImageViews = new ImageView[4];
    private LinearLayout mFootLayout;
    /**
     * Data
     */
    private RecommandHeadValue mHeadValue;

    public HomeHeaderLayout(Context context, RecommandHeadValue headValue) {
        super(context);
        this.mContext = context;
        this.mHeadValue = headValue;

        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_home_header_layout, this);
        mViewPager = (AutoScrollViewPager) mRootView.findViewById(R.id.pager);
        mPagerIndictor = (CirclePageIndicator) mRootView.findViewById(R.id.pager_indictor_view);
        mHotView = (TextView) mRootView.findViewById(R.id.zuixing_view);
        mImageViews[0] = (ImageView) mRootView.findViewById(R.id.head_image_one);
        mImageViews[1] = (ImageView) mRootView.findViewById(R.id.head_image_two);
        mImageViews[2] = (ImageView) mRootView.findViewById(R.id.head_image_three);
        mImageViews[3] = (ImageView) mRootView.findViewById(R.id.head_image_four);
        mFootLayout = (LinearLayout) mRootView.findViewById(R.id.content_layout);

        mAdapter = new PhotoViewPagerAdapter(mContext, mHeadValue.getAds(), true);
        mViewPager.setAdapter(mAdapter);
        mViewPager.startAutoScroll(3000);
        mPagerIndictor.setViewPager(mViewPager);

        mRootView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, StoryDetailActivity.class);
                mContext.startActivity(intent);
            }
        });

        for (int i = 0; i < mImageViews.length; i++) {
            GlideManager.loadImageView(mContext, mHeadValue.getMiddle().get(i), mImageViews[i]);
        }

        for (RecommandFooterValue value : mHeadValue.getFooter()) {
            mFootLayout.addView(createItem(value));
        }
        mHotView.setText(mContext.getString(R.string.today_zuixing));
    }

    private View createItem(RecommandFooterValue value) {
        HomeBottomItem bottomItem = new HomeBottomItem(mContext, value);

        return bottomItem;
    }
}
